<template>
    <div class="edit-dialog-container">
        <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px" destroy-on-close>
            <el-form ref="dialogFormRef" :model="state.content" :rules="formRules" size="default" label-width="90px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="code" label="任务编号">
                            <el-input v-model="state.content.code" :disabled="state.dialog.type === 1" placeholder="请输入任务编号"
                                clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="name" label="任务名称">
                            <el-input v-model="state.content.name" placeholder="请输入任务名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="name" label="任务类型">
                            <el-select @change="state.content.eventTrigger = []" v-model="state.content.TType"
                                placeholder="Select" style="width: 240px">
                                <el-option v-for="item in tType" :key="item.value" :label="item.label"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="name" label="启用状态">
                            <el-switch v-model="state.content.enable" :active-value="1" :inactive-value="2" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item prop="code" label="执行周期">
                            <el-checkbox-group v-if="state.content.TType === 1" v-model="state.content.eventTrigger">
                                <el-checkbox v-for="item in 24" :label=item>{{ `${item - 1}点` }}</el-checkbox>
                            </el-checkbox-group>
                            <el-checkbox-group v-if="state.content.TType === 2" v-model="state.content.eventTrigger">
                                <el-checkbox v-for="item in 7" :label=item>{{ `${item}天` }}</el-checkbox>
                            </el-checkbox-group>
                            <el-checkbox-group v-if="state.content.TType === 3" v-model="state.content.eventTrigger">
                                <el-checkbox v-for="item in 31" :label=item>{{ `${item}号` }}</el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onSubmitContinue" type="primary" size="default" v-if="state.dialog.type === 0">{{
                        t('message.btnName.continueSubmit')
                    }}</el-button>
                    <el-button @click="onSubmit" type="success" size="default">{{ t('message.btnName.submit')
                    }}</el-button>
                    <el-button @click="onCancel" size="default">{{ t('message.btnName.cancel') }}</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts" >
import { reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { ElMessage } from 'element-plus';
import { usePersonRoleCreateApi } from '/@/api/person/role';
import { useSystemTaskQueryApi, useSystemTaskUpdateApi } from '/@/api/system/task';

// 定义子组件向父组件传值/事件
const { t } = useI18n();
const emit = defineEmits(['refresh']);

// 定义变量内容
const dialogFormRef = ref();
const tType = [
    {
        value: 1,
        label: '按天',
    },
    {
        value: 2,
        label: '按周',
    },
    {
        value: 3,
        label: '按月',
    },
]

const useContent = () => {
    return {
        code: '', // 账户名称
        name: '', // 用户姓名
        TType: null,
        enable: 1,
        eventTrigger: []
    }
}

const state = reactive({
    content: {} as any,
    dialog: {
        isShowDialog: false,
        type: 0,
        title: '',
    },
});

// 表单校验
const formRules = ref({
    code: [
        { required: true, message: '必填项不能为空', trigger: 'blur' },
        { min: 1, max: 40, message: '工号必须1-40位字母数字组合而成', trigger: 'blur' },
        // { pattern: /^[a-z1-9]/, message: '工号必须非0数字或小写字母开头', trigger: 'blur' },
    ],
    name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    mobile: [{ pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/, message: '请输入正确手机号', trigger: 'blur' },],
})



// 打开弹窗
const openDialog = async (code?: string) => {
    state.dialog.isShowDialog = true
    if (code) {
        state.dialog.title = t('message.btnName.edit')
        state.dialog.type = 1
        queryItem(code)
    }
};
// 关闭弹窗
const closeDialog = () => {
    state.dialog.isShowDialog = false;
    state.content = useContent()
    emit('refresh');
};
// 取消
const onCancel = () => {
    closeDialog();
};
const queryItem = (code: string) => {
    useSystemTaskQueryApi(code).then(({ content }) => {
        if (content.TType === 1) {
            state.content = content
            state.content.eventTrigger = state.content.eventTrigger.map((item: number) => {
                return item + 1
            })
        } else {
            state.content = content
        }

    })
}
// 提交
const onSubmit = () => {
    let data = JSON.parse(JSON.stringify(state.content))
    if (data.TType === 1) {
        data.eventTrigger = data.eventTrigger.map((item: number) => {
            return item - 1
        })
    }
    useSystemTaskUpdateApi(data).then(({ content }) => {
        closeDialog()
        ElMessage({
            type: 'success',
            message: '操作成功'
        })
    })
};
// 提交
const onSubmitContinue = () => {
    usePersonRoleCreateApi(state.content).then(() => {
        ElMessage({
            type: 'success',
            message: '新增成功'
        })
    }).then(() => {
        state.content = useContent()
    })
};

defineExpose({
    openDialog,
});
</script>


<style scoped lang="scss">
:deep(.el-dialog__header) {
    margin: 0;
    border-bottom: 1px dashed rgba(0, 0, 0, .2);
}

:deep(.el-dialog__footer) {
    border-top: 1px dashed rgba(0, 0, 0, .2);
}
</style>